﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMProductMtn : BaseMtn 
    {
        public frmRMProductMtn()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MtnTabControl = this.tbcMain;
            this.DesignTabsName = "tbpGeneral";
            tbcMain.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MinimumSize = this.Size;
        }



        #region Define Variable
        private bool mblnSettingStatus = false;
        #endregion

        #region ***Form Events

        private void frmRMProductMtn_Load(object sender, EventArgs e)
        {
            this.TableName = "rmProduct";
            //dgList.ColumnWidth = "0,70,180,100,40,20,120,90,130";
            dgList.ConnectionString = DBAccess.ConnectionDBString;
            dgList.TableName = this.TableName;
            dgList.ColumnWidth = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgList.Name);
            string strSql = WP.Commons.BaseFunctions.GetLookUpSQL(this.TableName, this.CurrentMenuInfo.iParameter);
            dgList.Sql = strSql;
            this.formStatus = FormStatus.Query;

            FillLookUpInfo();
        }
        private void frmRMProductMtn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgList);
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!this.UpdateData()) return;
            base.SetStatusAfterUpdate();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (this.ConfirmChange())
            {
                if (!this.UpdateData()) return;
            }
            this.formStatus = FormStatus.AddNew;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!this.DeleteData()) return;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.formStatus = FormStatus.Delete;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
            {
                if (this.ConfirmChange())
                {
                    if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (!this.UpdateData()) return;
                    }
                }
            }
            this.ExitingStatus = true;
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void btnToExcel_Click(object sender, EventArgs e)
        {
            base.ExportToExcel(dgList);
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Next);
        }
        private void btnPrev_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Prev);
        }
        private void btnTop_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Top);
        }
        private void btnBottom_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Bottom);
        }
        public override void SetNavigatorBarTooltip()
        {
            if (this.RecID <= 0)
            {
                this.btnBottom.ToolTipText = "定位到末页";
                this.btnTop.ToolTipText = "定位到首页";
                this.btnNext.ToolTipText = "定位到下一页";
                this.btnPrev.ToolTipText = "定位到前一页";
            }
            else
            {
                this.btnBottom.ToolTipText = "定位到末记录";
                this.btnTop.ToolTipText = "定位到首记录";
                this.btnNext.ToolTipText = "定位到下一条记录";
                this.btnPrev.ToolTipText = "定位到上一条记录";
            }
        }

        private void dgList_DefineColumn(object sender, EventArgs e)
        {
            base.SelectListViewField(dgList);
        }
        private void dgList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && dgList.CurrentRow != null)
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }
        private void dgList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgList.CurrentRow != null && BaseFunctions.IsDataGridCell(this.dgList, e))
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }

        protected override void Field_Leave(object sender, System.EventArgs e)
        {
            base.Field_Leave(sender, e);
            Control con = sender as Control;
            if (con == null) return;

            //below is some speci field , need do some speci work
            
        }
        protected override void Field_Change(object sender, EventArgs e)
        {
            base.Field_Change(sender, e);
            if (this.formStatus == FormStatus.Delete) return;
            Control con = sender as Control;
            if (con == null) return;
            DBAccess dbcon = new DBAccess(false, false);
            if (con.Name == txtSetName.Name && this.formStatus == FormStatus.AddNew)
            {

                string strSQL = string.Empty;
                strSQL = "select * from rmSet where SetName ='" + con.Text + "'";
                DataRow drSet = dbcon.GetDataFirstRow(strSQL);
                if (drSet != null)
                {
                    txtSetSize.Text = CvtFunc.ToString(drSet["SetSize"]);
                    txtProductLength.Text = CvtFunc.ToString(drSet["Length"]);
                    txtProductWidth.Text = CvtFunc.ToString(drSet["Width"]);
                }

            }

            if ((con.Name == txtCategoryName.Name || con.Name == txtSetName.Name) && this.formStatus == FormStatus.AddNew)
            {
                string strSQL = string.Empty;
                string strCategoryName = txtCategoryName.Text;
                string strSetName = txtSetName.Text;
                strSQL = "select * from rmCategorySetBOMHdr where CategoryName='" + strCategoryName + "' and SetName='" + strSetName + "'";
                DataTable dtHdr = dbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtHdr.Rows)
                {
                    txtQuantity.Text = BaseFunctions.FormatNumeric( drRow["Quantity"],"Ns.2");
                    txtSetSize.Text = BaseFunctions.FormatNumeric(drRow["SetSize"], "Ns.2");
                    txtProductLength.Text = BaseFunctions.FormatNumeric(drRow["ProductLength"], "Ns.2");
                    txtProductWidth.Text = BaseFunctions.FormatNumeric(drRow["ProductWidth"], "Ns.2");
                    txtPaperHeight.Text = BaseFunctions.FormatNumeric(drRow["PaperHeight"], "Ns.2");
                    txtPages.Text = BaseFunctions.FormatNumeric(drRow["Pages"], "Ns.2");

                    txtLabelUnitQty.Text = BaseFunctions.FormatNumeric(drRow["LabelUnitQty"], "Ns.2");
                    txtLabelLength.Text = BaseFunctions.FormatNumeric(drRow["LabelLength"], "Ns.2");
                    txtLabelWidth.Text = BaseFunctions.FormatNumeric(drRow["LabelWidth"], "Ns.2");
                    txtLabelSize.Text = BaseFunctions.FormatNumeric(drRow["LabelSize"], "Ns.0");
                    
                    txtPrintLength.Text = BaseFunctions.FormatNumeric(drRow["PrintLength"], "Ns.2");
                    txtPrintWidth.Text = BaseFunctions.FormatNumeric(drRow["PrintWidth"], "Ns.2");

                    txtLaminatingLength.Text = BaseFunctions.FormatNumeric(drRow["LaminatingLength"], "Ns.2");
                    txtLaminatingWidth.Text = BaseFunctions.FormatNumeric(drRow["LaminatingWidth"], "Ns.2");
                    txtCutFactor.Text = BaseFunctions.FormatNumeric(drRow["CutFactor"], "Ns.2");
                    txtPrintUseQty.Text = BaseFunctions.FormatNumeric(drRow["PrintUseQty"], "Ns.2");
                    txtContainQty.Text = BaseFunctions.FormatNumeric(drRow["ContainQty"], "Ns.2");
                    txtPackageQty.Text = BaseFunctions.FormatNumeric(drRow["PackageQty"], "Ns.2");
                    txtInnerDiameterLength.Text = BaseFunctions.FormatNumeric(drRow["InnerDiameterLength"], "Ns.2");
                    txtInnerDiameterWidth.Text = BaseFunctions.FormatNumeric(drRow["InnerDiameterWidth"], "Ns.2");
                    txtInnerDiameterHeight.Text = BaseFunctions.FormatNumeric(drRow["InnerDiameterHeight"], "Ns.2");
                    txtInnerOutsideDiff.Text = BaseFunctions.FormatNumeric(drRow["InnerOutsideDiff"], "Ns.2");
                    txtMiddleWidth.Text = BaseFunctions.FormatNumeric(drRow["MiddleWidth"], "Ns.2");
                    txtBurrWidth.Text = BaseFunctions.FormatNumeric(drRow["BurrWidth"], "Ns.2");
                    txtOtherPrintWidth.Text = BaseFunctions.FormatNumeric(drRow["OtherPrintWidth"], "Ns.2");
                    txtOtherPrintBackWidth.Text = BaseFunctions.FormatNumeric(drRow["OtherPrintBackWidth"], "Ns.2");
                    txtUseIronNailQty.Text = BaseFunctions.FormatNumeric(drRow["UseIronNailQty"], "Ns.2");
                    txtLockingWireQty.Text = BaseFunctions.FormatNumeric(drRow["LockingWireQty"], "Ns.2");
                    txtLockingWireLength.Text = BaseFunctions.FormatNumeric(drRow["LockingWireLength"], "Ns.2");
                }
            }
            dbcon.CloseCon();
        }
        protected override void Field_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.Field_KeyPress(sender, e);
        }

        private void tbcMain_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if ((this.formStatus == FormStatus.Query || this.formStatus == FormStatus.Delete) &&
                this.dgList.Rows.Count == 0 && e.TabPageIndex > 0)
            {
                e.Cancel = true;
                return;
            }

            if (e.TabPageIndex == 0)
            {
                if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
                {
                    if (this.ConfirmChange())
                    {
                        if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            if (!this.UpdateData())
                            {
                                if (m_bTabPageNeedCancel)
                                    e.Cancel = true;

                                m_bTabPageNeedCancel = true;
                                return;
                            }
                        }
                    }
                    this.formStatus = FormStatus.Delete;
                }
                else if (this.formStatus == FormStatus.Query)
                {
                    this.formStatus = FormStatus.Delete;
                }
                this.dgList.RefreshData();
                this.dgList.Focus();
            }
            else if (e.TabPage == tbpRMInfo && this.formStatus == FormStatus.AddNew )
            {
                this.UpdateData();
            }
            else if (e.TabPageIndex != 0 && this.formStatus != FormStatus.AddNew)
            {
                if (this.RecID == -1)
                    this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }
        private void tbcMain_DrawItem(object sender, DrawItemEventArgs e)
        {
            BaseFunctions.TabControl_DrawItem(sender, e, true);
        }
        private void FileMenu_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mnuItem = sender as ToolStripMenuItem;
            if (mnuItem != null)
            {
                switch (mnuItem.Name)
                {
                    case "newToolStripMenuItem":
                        btnNew_Click(sender, e);
                        break;
                    case "cancelToolStripMenuItem":
                        btnCancel_Click(sender, e);
                        break;
                    case "deleteToolStripMenuItem":
                        btnDelete_Click(sender, e);
                        break;
                    case "saveToolStripMenuItem":
                        btnUpdate_Click(sender, e);
                        break;
                    case "exitToolStripMenuItem":
                        btnExit_Click(sender, e);
                        break;
                }
            }
        }

        #endregion Form Events***

        #region ***Form Functions

        public override bool UpdateData()
        {
            bool bUpdateResult = true;
            if (base.ConfirmChange())
            {
                bUpdateResult = base.UpdateData();
                RMProductBOMHdrDAL rmDAL = new RMProductBOMHdrDAL();
                DBAccess dbcon = new DBAccess(false, false);
                try
                {
                    dbcon.BeginTran();
                    string strSQL = "select * from rmProductBOMDetail where productname='" + txtProductName.Text + "'";
                    DataTable dtBOM = dbcon.GetDataTable(strSQL);
                    foreach (DataRow drBOM in dtBOM.Rows)
                    {
                        rmDAL.UpdateBOMDetail(dbcon, drBOM);
                    }

                    strSQL ="select * from rmProductBOMLabourCost where productname='"+ txtProductName.Text  +"'";
                    DataTable dtLabourCost = dbcon.GetDataTable(strSQL);
                    foreach (DataRow drLabourCost in dtLabourCost.Rows)
                    {
                        rmDAL.UpdateLabourCostDetail(dbcon, drLabourCost); 
                    }
                    dbcon.CommitTran(); 
                }
                catch (Exception ex)
                {
                    dbcon.RollbackTran();
                    MessageBox.Show(ex.ToString());
                    return false;
                }
            }
            return bUpdateResult;
        }
        public override bool ConfirmChange()
        {
            return (base.ConfirmChange());
        }
        protected override bool FieldCheck(string strFieldValue, string strFieldName, ref string strErrPrompt)
        {

            strErrPrompt = string.Empty;
            DBAccess dBAccess = new DBAccess(false, false);
            DataRow dr = base.GetCurrentMtnDataRow();


            RM.BusinessRule.RMProductBRL mBRL = new WP.RM.BusinessRule.RMProductBRL();
            bool bCheckResult = mBRL.FieldCheck(dBAccess, dr, strFieldName, ref strErrPrompt, this.formStatus);
            dBAccess.CloseCon();
            return bCheckResult;

        }
        protected override string GetLookUpSQL(string strTableName, string strLookUpControlName)
        {
            string strSql = string.Empty;
            return base.GetLookUpSQL(strTableName, strLookUpControlName);
        }
        public override void SetButtonStatus()
        {
            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = true;
                    this.btnCancel.Enabled = true;
                    break;
                case FormStatus.Delete:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    break;
                case FormStatus.Query:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    break;
                case FormStatus.Update:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = true;
                    this.btnNew.Enabled = true;
                    this.btnCancel.Enabled = true;
                    break;
                case FormStatus.Loading:
                    break;

            }

            this.btnToExcel.Visible = (this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query) && SysVar.AbleToExcel;
            this.btnToExcel.Enabled = this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query;

            newToolStripMenuItem.Enabled = btnNew.Enabled;
            cancelToolStripMenuItem.Enabled = btnCancel.Enabled;
            deleteToolStripMenuItem.Enabled = btnDelete.Enabled;
            saveToolStripMenuItem.Enabled = btnUpdate.Enabled;

            this.btnTop.Enabled = true;
            this.btnBottom.Enabled = true;
            this.btnPrev.Enabled = true;
            this.btnNext.Enabled = true;
            this.btnHelp.Enabled = true;
            this.btnExit.Enabled = true;
            this.btnLookStandardBOM.Enabled = true;
            this.btnLookStandardBOM.Visible = false;
        }

        public override void SetOtherStatus()
        {
            mblnSettingStatus = true;

            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    this.txtProductID.Focus();
                    break;
                case FormStatus.Delete:
                    break;
                case FormStatus.Query:
                    break;
                case FormStatus.Update:
                    break;
                case FormStatus.Loading:
                    break;
            }

            mblnSettingStatus = false;
        }
        public override void RefreshOverView()
        {
            base.RefreshOverView();
            if (this.RecID != -1)
                this.dgList.RecID = this.RecID;
        }

        private void FillLookUpInfo()
        {
            SetFieldLookUpInfo("CategoryID", "rmCategory", "CategoryID", "ubtCategoryName");
            SetFieldLookUpInfo("CategoryName", "rmCategory", "CategoryName", "ubtCategoryName");
            SetFieldLookUpInfo("SetID", "rmSet", "SetID", "ubtSetName");
            SetFieldLookUpInfo("SetName", "rmSet", "SetName", "ubtSetName");
        }

        #endregion Form Functions***

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string strMenuID = DataFunctions.GetFieldValue ("mMenu","MenuID","Net_ProgName='frmRMProductBOMMtn'");

            bool blnPermission = (Permission.GetMenuSecurity(strMenuID)=="Y" ? true : false);
            if (!blnPermission)
            {
                MsgBoxEx.Show("您可能没有权限，请联系系统管理员", SysVar.WarnTitle);
            }
            else
            {
                string strProductName = txtProductName.Text;
                if (this.formStatus == FormStatus.Query || this.formStatus == FormStatus.Delete )
                {
                    if (this.dgList.CurrentRow == null) return;
                    strProductName = CvtFunc.ToString(this.dgList.CurrentRow.Cells["ProductName"].Value);                    
                }
                if (strProductName == "") return;
                string strRecID = DataFunctions.GetFieldValue("rmProduct", "RecID", "ProductName='" + strProductName + "'");
                MenuInfo menuInfo = DataFunctions.GetMenuInfo(DataFunctions.GetFieldValue( "mMenu", "MenuId", "net_ProgName='frmRMProductBOMMtn' and net_Parameter=0"));
                ReflectMethod.ShowForm(menuInfo, FormStatus.Update, CvtFunc.ToLong(strRecID));
            }
        }       
    }
}
